package airthmetic.exercise.dp;

public class _309_买卖股票的最佳时机含冷冻期 {
    public static int maxProfit(int[] prices) {
        int n = prices.length;
        int[][] dp = new int[n][2];
        for(int i=0; i<n; i++){
            if(i==0){
                dp[i][0] = 0;
                dp[i][1] = -prices[i];
                continue;
            }

            dp[i][0] = Math.max(dp[i-1][0], dp[i-1][1] + prices[i]);
            if(i<=1){
                dp[i][1] = Math.max(dp[i-1][1], -prices[i]);
            }else{
                dp[i][1] = Math.max(dp[i-1][1], dp[i-2][0] - prices[i]);
            }

        }

        return dp[n-1][0];
    }

    public static int maxProfit2(int[] prices) {
        int n = prices.length;
        int i_0 = 0;
        int i_1 = Integer.MIN_VALUE;
        int i_2 = 0;
        for(int i=0; i<n; i++){
            int temp = i_0;
            i_0 = Math.max(i_0, i_1 + prices[i]);
            i_1 = Math.max(i_1, i_2 - prices[i]);
            i_2 = temp;
        }

        return i_0;
    }

    public static void main(String[] args) {
        System.out.println(maxProfit(new int[]{7,1,5,3,6,4}));
    }
}
